/**
 * @FileDescription 创建地图要素
 * @Author 祝小文
 * @Date 2022-10-09 10:03
 */
import cg_icon from '@/assets/image/MapIcon/cg-icon.png'
import sb_icon from '@/assets/image/MapIcon/sb-icon.png'
import hj_icon from '@/assets/image/mapIcon/hj-icon.png'
import xfbf_icon from '@/assets/image/MapIcon/xfbf-icon.png'
import pmz_icon from '@/assets/image/MapIcon/pmz-icon.png'
import xfsy_icon from '@/assets/image/MapIcon/xfsy-icon.png'
import trsy_icon from '@/assets/image/MapIcon/trsy-icon.png'
import crk_icon from '@/assets/image/mapIcon/crk-icon.png'
import dcskzs_icon from '@/assets/image/mapIcon/dcskzs-icon.png'
import xfkzs_icon from '@/assets/image/mapIcon/xfkzs-icon.png'
import jksp_icon from '@/assets/image/mapIcon/jksp-icon.png'
import yjsgc_icon from '@/assets/image/mapIcon/yjsgc-icon.png'

import xhs_icon from '@/assets/image/MapIcon/xhs-icon.png'
import pms_icon from '@/assets/image/MapIcon/pms-icon.png'
import gdsp_icon from '@/assets/image/MapIcon/gdsp-icon.png'
import gdpmp_icon from '@/assets/image/MapIcon/gdpmp-icon.png'
import qyzzd_icon from '@/assets/image/mapIcon/qyzzd-icon.png'

import xk_4_icon from '@/assets/image/mapIcon/xk-4.png'
import xk_xy_4_icon from '@/assets/image/mapIcon/xk-xy-4.png'
import xk_5_icon from '@/assets/image/mapIcon/xk-5.png'
import xk_6_icon from '@/assets/image/mapIcon/xk-6.png'
import xk_7_icon from '@/assets/image/mapIcon/xk-7.png'
import xk_8_icon from '@/assets/image/mapIcon/xk-8.png'
import xk_9_icon from '@/assets/image/mapIcon/xk-9.png'
import xk_10_icon from '@/assets/image/mapIcon/xk-10.png'
import xk_dy_10_icon from '@/assets/image/mapIcon/xk-dy-10.png'

/** 加载全部地图元素 */
export function loadAllMapFactor(data: any) {
  //要素类型的字典代码
  const overlayTypeArr = ['6', '9', '13', '17', '18', '19', '20', '25', '27', '28', '32', '34']
  const scrollWallTypeArr = ['4', '5', '7', '8', '10', '11', '12']
  const pointTypeArr = ['21', '22', '23', '24', '26']
  const rectTypeArr = ['30']

  data.forEach((item: any) => {
    //加载标牌
    if (overlayTypeArr.includes(item.bzlx)) {
      //1. 判断当前要素是子级目录的话 parentName绑定fid，2. 获取标牌图标
      let iconUrl
      let parentName = item.cjid
      let bzmc = item.bzmc

      switch (item.bzlx) {
        //储罐
        case '6':
          iconUrl = cg_icon
          parentName = item.fid
          bzmc = `${item.bh} ${item.bzmc}`
          break
        //设备
        case '9':
          iconUrl = sb_icon
          parentName = item.fid
          bzmc = `${item.bh} ${item.bzmc}`
          break
        case '13':
          parentName = item.fid
          iconUrl = hj_icon
          break
        case '17':
          iconUrl = xfbf_icon
          break
        case '18':
          iconUrl = pmz_icon
          break
        case '19':
          iconUrl = xfsy_icon
          break
        case '20':
          iconUrl = trsy_icon
          break
        case '25':
          iconUrl = crk_icon
          break
        case '27':
          iconUrl = dcskzs_icon
          break
        case '28':
          iconUrl = xfkzs_icon
          break
        case '32':
          iconUrl = qyzzd_icon
          break
        case '34':
          iconUrl = jksp_icon
          break
      }

      //位置信息
      const wzxx = JSON.parse(item.wzxx)

      createOverlay({
        id: item.cjid,
        parentName: parentName,
        iconUrl,
        bzlx: item.bzlx,
        text: bzmc,
        parentMenuId: item.parentId,
        pos: [wzxx.lng, wzxx.lat, wzxx.alt],
        attribute: item,
      })
    }
    //加载围栏
    else if (scrollWallTypeArr.includes(item.bzlx)) {
      //1. 判断当前要素是子级目录的话 parentName绑定fid
      let parentName = item.cjid
      switch (item.bzlx) {
        //单元
        case '8':
          parentName = item.fid
          break
      }

      if (item.wzxx) {
        //位置信息
        let wzxx = JSON.parse(item.wzxx)
        wzxx = wzxx.map((item: any) => {
          return [item.lng, item.lat, item.alt]
        })

        createScrollWall({
          id: item.cjid,
          parentName: parentName,
          pos: wzxx,
          text: item.bzmc,
          attribute: item,
        })
      }
    }
    //加载点图标
    else if (pointTypeArr.includes(item.bzlx)) {
      //获取图标
      let mapIconUrl
      switch (item.bzlx) {
        case '21':
          mapIconUrl = xhs_icon
          break
        case '22':
          mapIconUrl = pms_icon
          break
        case '23':
          mapIconUrl = gdsp_icon
          break
        case '24':
          mapIconUrl = gdpmp_icon
          break
        case '26':
          mapIconUrl = yjsgc_icon
          break
      }

      //位置信息
      const wzxx = JSON.parse(item.wzxx)

      createPoint({
        id: item.cjid,
        parentName: item.cjid,
        iconUrl: mapIconUrl,
        pos: [wzxx.lng, wzxx.lat, wzxx.alt],
        attribute: item,
      })
    }
    //加载路宽
    else if (rectTypeArr.includes(item.bzlx)) {
      //获取图标
      let mapIconUrl
      switch (Number(item.bz)) {
        case 3:
          mapIconUrl = xk_4_icon
          break
        case 4:
          mapIconUrl = xk_xy_4_icon
          break
        case 5:
          mapIconUrl = xk_5_icon
          break
        case 6:
          mapIconUrl = xk_6_icon
          break
        case 7:
          mapIconUrl = xk_7_icon
          break
        case 8:
          mapIconUrl = xk_8_icon
          break
        case 9:
          mapIconUrl = xk_9_icon
          break
        case 10:
          mapIconUrl = xk_10_icon
          break
        case 11:
          mapIconUrl = xk_dy_10_icon
          break
      }

      //位置信息
      const wzxx = JSON.parse(item.wzxx)

      createRect({
        id: item.cjid,
        parentName: item.cjid,
        iconUrl: mapIconUrl,
        pos: {
          lng: wzxx.lng,
          lat: wzxx.lat,
          height: wzxx.alt,
        },
        attribute: item,
      })
    }
  })
}

/** 创建标牌 */
export function createOverlay(option: any) {
  const el = document.createElement('div')

  // 标牌禁止右击
  el.setAttribute('oncontextmenu', 'return false')

  //消防设施标牌背景色
  if (option.parentMenuId === 'ys00001') {
    el.style.backgroundColor = '#40b2ff'
  }
  //企业设施标牌背景色
  else if (option.parentMenuId === 'ys00002') {
    el.style.backgroundColor = '#4CC884'
  }

  //消防控制室颜色
  if (option.bzlx === '28') {
    el.style.backgroundColor = '#40b2ff'
  }

  if (option.bzlx === '6' || option.bzlx === '9' || option.bzlx === '13' || option.bzlx === '32') {
    el.className = 'maplabelpopup-one'
  } else {
    el.className = 'maplabelpopup-two'
  }

  const img = document.createElement('img')
  img.src = option.iconUrl
  const div = document.createElement('div')
  div.innerText = option.text
  el.appendChild(img)
  el.appendChild(div)

  window.GOverlay.add({
    name: option.id,
    parentName: option.parentName,
    option: {
      position: option.pos,
      positioning: 'bottom-center',
      elementName: el,
      offset: [0, 0],
      attribute: option.attribute,
    },
  })
}

/** 创建围栏 */
export function createScrollWall(option: any) {
  //计算围栏中心点
  const cenPos = calCulatePosCenter(option.pos)

  const zxdjdwd = { lng: cenPos[0], lat: cenPos[1], alt: cenPos[2] + 10 }

  return window.GScrollWall.add({
    name: option.id,
    parentName: option.parentName,
    option: {
      positions: option.pos,
      diffHeight: 6, //墙高
      color: 'red',
      speed: 10,
      reverse: false,
      show: true,
      attribute: option.attribute,
    },
    labeloption: {
      name: option.text,
      position: [zxdjdwd.lng, zxdjdwd.lat, zxdjdwd.alt], //文本经纬度坐标
      labelfill: 'yellow',
      labelfont: '16px 微软雅黑',
      labeloffset: [0, -15], //标注偏移量
      labelEyeOffset: [0, 0, 10],
      showLabelBackground: true,
      labelBackgroundColor: 'rgba(1, 7, 17, 0.8)',
      labelBackgroundPadding: [20, 10],
    },
  })
}

/** 创建点图标 */
export function createPoint(option: any) {
  window.GPoint.add({
    name: option.id,
    parentName: option.parentName,
    option: {
      position: option.pos,
      url: option.iconUrl,
      scale: 0.7,
      attribute: option.attribute,
    },
  })
}

/** 创建路宽 */
export function createRect(option: any) {
  window.GCgsRect.add({
    name: option.id,
    parentName: option.parentName,
    option: {
      pos: option.pos,
      imgUrl: option.iconUrl,
      attribute: option.attribute,
    },
  })
}

/** 传入经纬度计算中心点 */
function calCulatePosCenter(pos: any) {
  let xAxios = 0,
    yAxios = 0,
    zAxios = 0,
    cenPos
  pos.forEach((item: any) => {
    xAxios += item[0]
    yAxios += item[1]
    zAxios += item[2]
  })
  cenPos = [xAxios / pos.length, yAxios / pos.length, zAxios / pos.length]
  return cenPos
}
